<template>
  <div>
    <el-card>
      <div class="header">
        <el-button type="primary" @click="handleAdd">添加状态</el-button>
      </div>

      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="状态名称">
        </el-table-column>

        <el-table-column
          prop="date"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleNext"
        layout="total, prev, pager, next"
        :total="total">
      </el-pagination>
    </el-card>
    <create ref="create" @Ok="handleInit"></create>
    <edit ref="edit" @Ok="handleInit"></edit>
  </div>
</template>

<script>
  import {getStatusList, removeStatus} from "@/api/set";
  import Create from "./components/create";
  import Edit from "./components/edit";


  export default {
    name: "index.vue",
    components: {
      Create,
      Edit
    },
    data() {
      return {
        value: '',
        tableData: [],
        page: 1,
        size: 10,
        total: 1
      }
    },
    mounted() {
      this.handleInit()
    },
    methods: {
      handleInit() {
        getStatusList(this.page, this.size).then(res => {
          this.tableData = res.result.records
          this.total = Number(res.result.total)
        })
      },
      handleAdd() {
        this.$refs['create'].show()
      },
      handleEdit(id) {
        this.$refs['edit'].show1(id)
      },
      handleDelete(id) {
        removeStatus({"id": id}).then(res => {
          if (res.status === 200) {
            this.$message.success("删除成功")
            this.handleInit();
          }
        })
      },
      handleNext(e) {
        this.page = e
        this.handleInit()
      },
    }
  }
</script>

<style scoped>
  .header {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
</style>
